package LeetCode;

import java.util.*;

public class LC_049_GroupAnagrams {
    public static void main(String[] args) {

    }

    class Solution {
        public List<List<String>> groupAnagrams(String[] strs) {
            HashMap<String, List<String>> map = new HashMap<>();

            for (String s : strs) {
                char[] str2char = s.toCharArray();
                Arrays.sort(str2char);
                String sortedStr = String.valueOf(str2char);

                if (map.containsKey(sortedStr))
                    map.get(sortedStr).add(s);
                else {
                    ArrayList<String> tmp = new ArrayList<>();
                    tmp.add(s);
                    map.put(sortedStr, tmp);
                }
            }

            List<List<String>> result = new ArrayList<>();
            for (List<String> list : map.values()) {
                Collections.sort(list);
                result.add(list);
            }

            return result;
        }
    }
}
